package com.system.action;

import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Query;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.system.entity.Doc;
import com.system.entity.DocType;
import com.system.service.DocService;
import com.system.service.DocTypeService;

@Controller("docAction")
public class DocAction extends ActionSupport implements ModelDriven<Doc>{
	@Resource
	private DocService docService;
	@Resource
	private DocTypeService docTypeService;
	private String message;
	private Doc doc;
	@Override
	public Doc getModel() {
		if (doc == null) {
			doc = new Doc();
		}
		return doc;
	}
	
	public String doc_add () {
		if (doc.getDoc_title().equals("")) {
			setMessage("标题不可以为空");
			return INPUT;
		}
		int doc_type_id =Integer.parseInt(ServletActionContext.getRequest().getParameter("type_id"));
		DocType docType = docTypeService.queryById(doc_type_id);
		doc.setDocType(docType);
		//System.out.println(doc.getDoc_content()+"------------------------");
		docService.add(doc);
		return  SUCCESS;
	}
	
	public String get_doc_list() {
		List<Doc>docs = null;
		docs = docService.queryAll();
		ActionContext.getContext().getSession().put("docs", docs);
		List<DocType>docTypes =  docTypeService.queryAll();
		ActionContext.getContext().getSession().put("docTypes", docTypes);
		return SUCCESS;
	}
	
	public String getDocListByType() {
		int type_id = Integer.parseInt(ServletActionContext.getRequest().getParameter("type_id"));
		DocType docType = docTypeService.queryById(type_id);
		List<Doc>docs = docService.getDocListByType(docType);
		ActionContext.getContext().getSession().put("docs", docs);
		return SUCCESS;
	}
	
	public String get_doc_detail() {
		int doc_id =Integer.parseInt(ServletActionContext.getRequest().getParameter("doc_id"));
		Doc doc = docService.queryById(doc_id);
		List<DocType>docTypes =  docTypeService.queryAll();
		ActionContext.getContext().getSession().put("docTypes", docTypes);
		ActionContext.getContext().getSession().put("doc", doc);
		return SUCCESS;
	}
	
	public String doc_update() {
		int type_id =Integer.parseInt(ServletActionContext.getRequest().getParameter("type_id"));
		DocType docType = docTypeService.queryById(type_id);
		doc.setDocType(docType);
		docService.update(doc);
		ActionContext.getContext().getSession().put("doc", doc);
		return SUCCESS;
	}
	
	public String doc_delete() {
		docService.delete(doc);
		return SUCCESS;
	}
	
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}

}
